<template>
	<view class="user" :class="[isDark?'dark':'', $store.state.AppStyle]" :style="{'background-color':isDark?'#1c1c1c':'#f6f6f6','min-height':isDark?'100vh':'auto'}">
		<view class="header" :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar bg-white" :style="{'height': CustomBar + 'px','padding-top':StatusBar + 'px'}">
				<view class="action" @tap="back">
					<text class="cuIcon-back"></text>
				</view>
				<view class="content text-bold" :style="[{top:StatusBar + 'px'}]">
					{{$t('manage.usersedit.useredit')}}
				</view>
				<!--  #ifdef H5 || APP-PLUS -->
				<view class="action" @tap="userEdit">
					<button class="cu-btn round bg-blue">{{$t('global.save')}}</button>
				</view>
				<!--  #endif -->
			</view>
		</view>
		<view :style="[{padding:NavBar + 'px 10px 0px 10px'}]"></view>
		
		<form>
			<view class="cu-form-group margin-top">
				<view class="title">{{$t('global.username')}}</view>
				<input name="input" disabled="disabled" :value="name"/>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">{{$t('global.nickname')}}</view>
				<input placeholder="$t('global.plsnickname')" name="input" v-model="screenName"/>
			</view>
			<view class="cu-form-group">
				<view class="title">{{$t('manage.usersedit.sex')}}</view>
				<radio-group class="block" @change="RadioChange2">
					<label><text class="tn-margin-right-sm">{{$t('manage.usersedit.nodisplay')}}</text>
						<radio class="cyan tn-margin-right-lg" :class="url=='bm'?'checked':''"
							:checked="url=='bm'?true:false" value="bm"></radio>
					</label>
					<label><text class="tn-margin-right-sm">{{$t('manage.usersedit.male')}}</text>
						<radio class="cyan tn-margin-right-lg" :class="url=='man'?'checked':''"
							:checked="url=='man'?true:false" value="man"></radio>
					</label>
					<label><text class="tn-margin-right-sm">{{$t('manage.usersedit.woman')}}</text>
						<radio class="cyan tn-margin-right-lg" :class="url=='woman'?'checked':''"
							:checked="url=='woman'?true:false" value="woman"></radio>
					</label>
				</radio-group>
			</view>
			<view class="cu-form-group">
				<view class="title">{{$t('manage.usersedit.userasset')}}</view>
				<input name="input" disabled="disabled" :value="assets"/>
				<view class="action">
					<text class="text-blue" @tap="recharge">{{$t('manage.usersedit.chargbuckle')}}</text>
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">{{$t('manage.usersedit.userexperience')}}</view>
				<input placeholder="$t('manage.usersedit.plsuserexperience')" name="input" type="number" v-model="experience"/>
			</view>
			<view class="cu-form-group">
				<view class="title">{{$t('global.email')}}</view>
				<input placeholder="$t('global.plsemail')" name="input" v-model="mail"/>
			</view>
			<view class="cu-form-group">
				<view class="title">{{$t('global.phone')}}</view>
				<input placeholder="$('global.plsphone')" name="input" v-model="phone"/>
			</view>
			<view class="cu-form-group">
				<view class="title">{{$t('manage.usersedit.rank')}}</view>
				<input placeholder="$t('manage.usersedit.customizerank')" name="input" v-model="customize"/>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">{{$t('manage.usersedit.permissset')}}</view>
				<input :value="groupText" name="input" disabled="true"/>
				<view class="action">
					<text class="text-blue" @tap="showModal" data-target="groupModal">{{$t('global.edit')}}</text>
				</view>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">{{$t('global.password')}}</view>
				<input placeholder="$t('global.plspassword')" v-model="password" name="input"/>
			</view>
			<view class="cu-form-group">
				<view class="title">{{$t('global.surepassword')}}</view>
				<input placeholder="$t('global.plstrypassword')" v-model="repassword" name="input"/>
			</view>
		</form>
		<!--  #ifdef MP -->
		<view class="post-update bg-blue" @tap="userEdit">
			<text class="cuIcon-upload"></text>
		</view>
		<!--  #endif -->
		<view class="cu-modal" :class="modalName=='groupModal'?'show':''" @tap="hideModal">
			<view class="cu-dialog" @tap.stop="">
				<radio-group class="block" @change="RadioChange">
					<view class="cu-list menu text-left">
						<view class="cu-item" v-for="(item,index) in groupList" :key="index">
							<label class="flex justify-between align-center flex-sub">
								<view class="flex-sub">{{item.name}}</view>
								<radio class="round" :class="group==item.group?'checked':''" :checked="group==item.group?true:false" :value="item.group" :data-name="item.name"></radio>
							</label>
						</view>
					</view>
				</radio-group>
			</view>
		</view>
	</view>
</template>

<script>
	import { localStorage } from '../../js_sdk/mp-storage/mp-storage/index.js'
	import darkModeMixin from '@/utils/darkModeMixin.js'
	export default {
		mixins: [darkModeMixin],
		data() {
			return {
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar,
				NavBar:this.StatusBar +  this.CustomBar,
				AppStyle:this.$store.state.AppStyle,
				
				
				uid:0,
				assets:0,
				name:'',
				screenName:'',
				password:'',
				repassword:'',
				mail:'',
				url: '',
				customize:'',
				token:'',
				experience:0,
				phone:"",
				groupText: $t('manage.usersedit.contributor'),
				group:"contributor",
				groupList:[
					{
						name: $t('manage.usersedit.admin'),
						group:"administrator"
					},
					{
						name: $t('manage.usersedit.editor'),
						group:"editor"
					},
					{
						name: $t('manage.usersedit.contributor'),
						group:"contributor"
					},
					{
						name: $t('manage.usersedit.subscriber'),
						group:"subscriber"
					},
					{
						name: $t('manage.usersedit.visitor'),
						group:"visitor"
					},
				],
				modalName: null,
				radio: 'radio1',
			}
		},
		onPullDownRefresh(){
			
		},
		onShow(){
			// #ifdef APP-PLUS
			//plus.navigator.setStatusBarStyle("dark")
			// #endif
		},
		onLoad(res) {
			var that = this;
			// #ifdef APP-PLUS || MP
			that.NavBar = that.CustomBar;
			// #endif
			if(res.uid){
				that.uid = res.uid;
				that.getUserInfo();
			}
		},
		methods: {
			back(){
				uni.navigateBack({
					delta: 1
				});
			},
			getUserInfo(){
				var that = this;
				var token = "";
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				}
				that.$Net.request({
					url: that.$API.getUserInfo(),
					data:{
						"key":that.uid,
						"token":token
					},
					header:{
						'Content-Type':'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						if(res.data.code==1){
							that.name = res.data.data.name;
							that.screenName = res.data.data.screenName;
							that.mail = res.data.data.mail;
							that.url = res.data.data.url;
							that.group = res.data.data.groupKey;
							that.phone = res.data.data.phone;
							that.customize = res.data.data.customize;
							that.assets =  res.data.data.assets;
							that.experience=  res.data.data.experience;
							var list = that.groupList;
							for(var i in list){
								if(list[i].group == that.group){
									that.groupText = list[i].name;
								}
							}
						}
					}, 
					fail: function(res) {
						uni.showToast({
							title: that.$t('common.networkfail'),
							icon: 'none'
						})
					}
				})
			},
			RadioChange2(e) {
				this.url = e.detail.value
			},
			userEdit() {
				var that = this;
				if (that.password != "") {
					if (that.password != that.repassword) {
						uni.showToast({
						    title: that.$t('global.twopassdisagree'),
							icon:'none',
							duration: 1000,
							position:'bottom',
						});
						return false
					}
				}
				var token = "";
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				}
				var data = {
					uid:that.uid,
					name:that.name,
					screenName:that.screenName,
					password:that.password,
					mail:that.mail,
					url:that.url,
					customize:that.customize,
					group:that.group,
					experience:that.experience
				}
				uni.showLoading({
					title: that.$t('common.loadmore')
				});
				that.$Net.request({
					url: that.$API.manageUserEdit(),
					data:{
						"params":JSON.stringify(that.$API.removeObjectEmptyKey(data)),
						"token":token
					},
					header:{
						'Content-Type':'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						setTimeout(function () {
							uni.hideLoading();
						}, 1000);
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
						if(res.data.code==1){
							//保存用户信息
							var timer = setTimeout(function() {
								that.back();
								clearTimeout('timer')
							}, 1000)
							
						}
					},
					fail: function(res) {
						setTimeout(function () {
							uni.hideLoading();
						}, 1000);
						uni.showToast({
							title: that.$t('common.networkfail'),
							icon: 'none'
						})
						uni.stopPullDownRefresh()
					}
				})
			},
			showModal(e) {
				this.modalName = e.currentTarget.dataset.target
			},
			hideModal(e) {
				this.modalName = null
			},
			RadioChange(e) {
				var that = this;
				that.group = e.detail.value;
				var list = that.groupList;
				for(var i in list){
					if(list[i].group == that.group){
						that.groupText = list[i].name;
					}
				}
				that.hideModal();
			},
			recharge(){
				var that = this;
				localStorage.setItem('getuid',that.uid);
				uni.navigateTo({
				    url: '/pages/manage/recharge'
				});
			},
		}
	}
</script>